package com.cookie.meetproject.ui;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cookie.framework.base.BaseBackActivity;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.bmob.IMUser;
import com.cookie.framework.helper.FileHelper;
import com.cookie.framework.helper.GlideHelper;
import com.cookie.framework.view.LoadingView;
import com.cookie.meetproject.R;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import androidx.annotation.Nullable;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.ui
 * 文件名:    ShareImageActivity
 * 创建者:    cookie
 * 创建时间:   2020/5/25 23:01
 * 描述:      TODO
 */

public class ShareImageActivity extends BaseBackActivity implements View.OnClickListener {

    private ImageView ivPhoto;
    private TextView tvName;
    private TextView tvSex;
    private TextView tvAge;
    private TextView tvPhone;
    private TextView tvDesc;
    private ImageView ivQrcode;
    private LinearLayout llContent;
    private LinearLayout llDownload;

    private LoadingView mLoadingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_img);
        initView();
    }

    private void initView() {

        mLoadingView = new LoadingView(this);
        mLoadingView.setLoadingText("正在保存");

        ivPhoto = (ImageView) findViewById(R.id.iv_photo);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvSex = (TextView) findViewById(R.id.tv_sex);
        tvAge = (TextView) findViewById(R.id.tv_age);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvDesc = (TextView) findViewById(R.id.tv_desc);
        ivQrcode = (ImageView) findViewById(R.id.iv_qrcode);
        llContent = (LinearLayout) findViewById(R.id.ll_content);
        llDownload = (LinearLayout) findViewById(R.id.ll_download);

        llDownload.setOnClickListener(this);

        loadInfo();
    }

    @SuppressLint("SetTextI18n")
    private void loadInfo() {
        IMUser imUser = BmobManger.getInstance().getUser();

        GlideHelper.loadUrl(this, imUser.getPhoto(), ivPhoto);
        tvName.setText(imUser.getNickName());
        tvSex.setText(imUser.isSex() ? R.string.text_me_info_boy : R.string.text_me_info_girl);
        tvAge.setText(imUser.getAge() + getString(R.string.text_search_age));
        tvPhone.setText(imUser.getMobilePhoneNumber());
        tvDesc.setText(imUser.getDesc());

        createQRCode(imUser.getObjectId());
    }

    private void createQRCode(String userId) {

        ivQrcode.post(new Runnable() {
            @Override
            public void run() {
                String textContent = "Meet#" + userId;
                Bitmap bitmap = CodeUtils.createImage(textContent, ivQrcode.getWidth(), ivQrcode.getHeight(), null);
                ivQrcode.setImageBitmap(bitmap);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_download:
                /*
                 * 1view的截图
                 * 2创建bitmap
                 * 3保存到相册
                 */
                mLoadingView.show();
                //绘制的时候保存缓存
                llContent.setDrawingCacheEnabled(true);
                llContent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                llContent.layout(0, 0, llContent.getMeasuredWidth(), llContent.getMeasuredHeight());
                Bitmap bitmap = llContent.getDrawingCache();
                if (bitmap != null) {
                    FileHelper.getInstance().saveBitmapToAlbum(this,bitmap);
                    mLoadingView.hide();
                }
                break;
        }
    }


}
